#include "cfg.h"  
 
#define USER_CFG_FILE   "user.cfg"  
#define USERINFO_HEADER "[UserInfo]"  
 
typedef struct _userInfo  
{  
    char name[20];  
    int  ssno;  
    char sex[4];  
 
}UserInfo;  
 
int save_cfg( char *filename, const char *header, UserInfo *pInfo)  
{  
    FILE *fp = NULL;  
 
    if( ( fp = fopen(filename,"wa+") ) == NULL )  
    {  
        printf("cannot open file %s\n",filename);  
        return -1;  
    }  
 
    WRITE_PARAM_HEADER(fp, header);  
 
    SAVE_PARAM_STR(fp,name,pInfo->name);  
    SAVE_PARAM_INT(fp,ssno,pInfo->ssno);  
    SAVE_PARAM_STR(fp,sex,pInfo->sex);  
 
    if (fp)  
    {  
        fclose(fp);  
    }  
 
    return 0;  
}  
 
int read_cfg( char *filename, const char *header, UserInfo * pInfo )  
{  
    FILE *fp = NULL;  
    char keyName[100];  
 
    if( ( fp = fopen(filename,"rt") ) == NULL )  
    {  
        printf("cannot open file %s\n",filename);  
        return -1;  
    }  
 
    if( FIND_PARAM_HEADER(fp, header) != 0)  
    {  
        return -1;  
    }  
 
    LOAD_PARAM_STR(fp,keyName,pInfo->name);  
    LOAD_PARAM_INT(fp,keyName,&pInfo->ssno);  
    LOAD_PARAM_STR(fp,keyName,pInfo->sex);  
 
    if (fp)  
    {  
        fclose(fp);  
    }  
 
    return 0;  
}  
 
int main()  
{  
    UserInfo user = { "Ruby",118,"girl"};  
    UserInfo userRead;  
 
//    save_cfg(USER_CFG_FILE, "tt1", &user);  
 
    printf("enter any key to show user.cfg\n");  
 
    getchar();  
 
    //int ret = read_cfg(USER_CFG_FILE, USERINFO_HEADER, &userRead);  
    int ret = read_cfg(USER_CFG_FILE, "tt2", &userRead);  
    if (ret != -1) {
       printf("userInfo:\nname:%s\nssno:%d\nsex:%s\n",userRead.name,userRead.ssno,userRead.sex);  
    }
    ret = read_cfg(USER_CFG_FILE, "tt1", &userRead);  
    if (ret != -1) {
        printf("userInfo:\nname:%s\nssno:%d\nsex:%s\n",userRead.name,userRead.ssno,userRead.sex);  
    }
    getchar();  
    getchar();  

	return 0;
} 
